package com.cqjtu.education.service.impl;

import com.cqjtu.education.entity.Course;
import com.cqjtu.education.dao.CourseDao;
import com.cqjtu.education.service.CourseService;
import com.cqjtu.education.utils.AliyunOSS;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * (Course)表服务实现类
 *
 * @author makejava
 * @since 2022-04-13 14:11:03
 */
@Service("courseService")
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseDao courseDao;
    @Resource
    private AliyunOSS aliyunOSS;

    @Override
    public String uploadCourseImage(String cid, MultipartFile file) {
        String cover = aliyunOSS.uploadPublic(file);
        Course course = courseDao.queryById(cid);
        course.setCover(cover);
        courseDao.update(course);
        return cover;
    }

    @Override
    public List<Course> queryByTeacherId(String teacherId) {
        List<String> list = this.courseDao.queryByTeacherId(teacherId);
        List<Course> courses = new ArrayList<Course>();
        for (String s : list) {
            Course course = this.courseDao.queryById(s);
            courses.add(course);
        }
        return courses;
    }

    @Override
    public int link(List<String> tids, String cid) {
        for (String tid : tids) {
            courseDao.link(tid, cid);
        }
        return 0;
    }

    @Override
    public int linked(String tid, String cid) {
        return courseDao.link(tid, cid);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Course queryById(String id) {
        return this.courseDao.queryById(id);
    }

    @Override
    public List<Course> getAllCourses(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return courseDao.getAllCourses();
    }

    @Override
    public List<Course> queryByOpenTime(int pageNum, int pageSize) {
        Date now=new Date();
        PageHelper.startPage(pageNum, pageSize);
        return courseDao.queryByOpenTime(now,pageNum,pageSize);
    }

    @Override
    public List<Course> queryByPrice(Double low, Double high, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return courseDao.queryByPrice(low,high,pageNum,pageSize);
    }

    @Override
    public List<Course> fuzzyQuery(String key, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return this.courseDao.fuzzyQuery(key,pageNum,pageSize);
    }

    @Override
    public int countByKey(String key) {
        return this.courseDao.countByKey(key);
    }

    /**
     * 分页查询
     *
     * @param course 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */

    /**
     * 新增数据
     *
     * @param course 实例对象
     * @return 实例对象
     */
    @Override
    public Course insert(Course course) {
        String id= UUID.randomUUID().toString();
        course.setId(id);
        course.setCreateTime(new Date());
        this.courseDao.insert(course);
        return course;
    }

    /**
     * 修改数据
     *
     * @param course 实例对象
     * @return 实例对象
     */
    @Override
    public Course update(Course course) {
        this.courseDao.update(course);
        return this.queryById(course.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.courseDao.deleteById(id) > 0;
    }
}
